app.controller('goodsController' , function($scope, $http, utilService, $location){
	
	$scope.cols = [
		{name: '序号', column: '$index', width: '60px'},
		{name: '商品名称', column: 'name'},
		{name: '价格', column: 'price', width: '100px'},
		{name: '介绍', column: 'details'},
		{name: '类别', column: 'classify', width: '120px'}
	];
	
	var myModal = $('#myModal');
	$scope.pageArr = [];
	$scope.page = 0;
	$scope.size = 3;
	$scope.totalPage = 0;
	
	/*
	 *	商品展示
	 * */
	function showGoods(){
		
		utilService.ajax({
			url: '../../api/goods_list.php',
			data: {size: $scope.size,query:$scope.selecttxt,page: $scope.page},
			ok: function(response) {
				if (response.success) {
					console.log(response);
					$scope.goods = response.data;
					$scope.totalPage = Math.ceil(response.total / $scope.size);
					$scope.pageArr = _.range($scope.totalPage);
				} else {
					// ...
				}
			}
		}, $scope);
	}
	
	$scope.$on("$viewContentLoaded", function(){
		showGoods();
	});
	
//	分页点击跳转
	$scope.jumpPage = function() {
		$scope.page = this.cpage;
		showGoods();
	};
	
	/*
	 *	修改商品
	 * */
	$('#updateGoods').on('click', function() {
		
		var $checkboxs = $('wbc-table').find('input[type=checkbox]:checked');
		var length = $checkboxs.length;
		var id,obj;
		if(length !=1){
			layer.msg('只能一次修改一个商品，请重新选择', {
				offset: 't',
				anim: 6
			});
			return;
		}else{
			obj = _.findWhere($scope.goods, {id: $checkboxs[0].id * 1});
			console.log(obj);
			$scope.id = obj.id;
			$scope.goodsname = obj.name;
			$scope.goodsprice = obj.price;
			$scope.goodsdetail = obj.details;
			$scope.goodsclassify = obj.classify;
			myModal.modal('show');	
			myModal.find($('.modal-title')).html('修改商品');
		}
	});
	//模态框保存按钮
	$scope.saveGoods = function(){
		if($('.modal-title')[0].innerText=='修改商品'){
			var url = '../../api/goods_update.php';
		}else{
			var url = '../../api/goods_add.php';
		}
		
		utilService.ajax({
			url: url,
			method: 'post',
			data: {
				id: $scope.id,
				name: $scope.goodsname,
				price: $scope.goodsprice,
				details: $scope.goodsdetail,
				classify: $scope.goodsclassify
			},
			ok: function(response) {
				if (response.success) {
					showGoods();
					myModal.modal('hide');	
					myModal.find('form').trigger("reset");
				} else {
					// ...
				}
			}
		}, $scope);
	}
	
	/*
	 *	添加商品
	 * */
	$scope.addGoods = function(){
		myModal.find($('.modal-title')).html('添加商品');
		myModal.modal('show');		
	}
	
	/*
	 *	删除商品
	 * */
	$scope.delGoods = function(){
		var $checkboxs = $('wbc-table').find('input[type=checkbox]:checked');
		var length = $checkboxs.length;
		console.log(length);
		if(length<1){
			layer.msg('最少删除一个商品', {
				offset: 't',
				anim: 6
			});
			return;
		}
		layer.confirm('确定要删除该商品么？',{
			btn: ['确定', '取消']
		}, function(index){
			layer.close(index);
			utilService.ajax({
				url: '../../api/goods_del.php',
				method: 'post',
				data: {
					ids: $checkboxs[0].id * 1
				},
				ok: function(response) {
					if (response.success) {
						showGoods();
					} else {
						// ...
					}
				}
			}, $scope);
		});
	}
	
	/*
	 *	查询商品
	 * */
	$scope.selectgoods = function(){
		showGoods();
	}
	
});